#pragma once

#include "log.h"
#include "byteBuffer.h"
#include "blockNode.h"
#include "handler.h"

#include "ThreadSafeQueue.h"

class Register;

class Packet : public ByteBuffer
{
public:

	Packet(Register* reg = NULL);
	Packet(uint16 opcode, Register* reg = NULL);
	Packet(const Packet &r);

	virtual ~Packet() {}
	
	Packet& operator= (const Packet& r);

	uint16 GetOpcode() const		{ return _opcode; }	
	void   SetOpcode(uint16 opcode) { _opcode = opcode; }

	inline Register* GetRegister()  { return _register; }

	virtual void EncodePacket(){};
	virtual bool DecodePacket(const uint8* data, const uint16 size);

protected:

	uint16	  _opcode;
	Register* _register;
};

const int NET_PACKET_VERSI0N = 0x0100;
const int NET_PACKET_OFFSET = 2 + 2 + 2; //len + version + opcode 

class NetPacket : public Packet
{
public:

	NetPacket(Register* reg = NULL);
	NetPacket(uint16 opcode, Register* reg = NULL);
	NetPacket(const Packet &r);

	virtual ~NetPacket() {}
	
	NetPacket& operator= (const NetPacket& r);

	virtual void EncodePacket();
	virtual bool DecodePacket(const uint8* data, const uint16 size);
};

typedef ThreadSafeQueue<Packet>		PakcetQueue;